/**
 * MainFrame.java
 * 
 * Main frame for the Home Energy Prediction System. Run this class to start the application.
 * 
 */
package com.swin.energy;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;

import com.swin.energy.DataGenerator.RUN_STATE;

class MainFrame extends JFrame implements ActionListener {

	private static final long serialVersionUID = -6372980139148510684L;

	private JMenuBar menubar;
	private JMenu viewmenu;
	private JMenu filemenu;

	Initialisation init = new Initialisation();

	private MapPanel mapPanel;
	private GraphPanel pastData;
	private GraphPanel neuralNetworkError;
	private GraphPanel actualData;

	private OptionsPanel optionsPanel;

	private JMenuItem newWorldItem;
	private JMenuItem quitItem;
	private JMenuItem viewWorldItem;

	private InitialisationPanel initPanel;

	DataGenerator generator;
	List<String> areaData;

	private void createMenu() {

		filemenu = new JMenu("File");
		filemenu.add(new JSeparator());
		viewmenu = new JMenu("View");
		viewmenu.add(new JSeparator());
		newWorldItem = new JMenuItem("New World...");
		newWorldItem.addActionListener(this);
		newWorldItem.add(new JSeparator());
		quitItem = new JMenuItem("Exit");
		quitItem.addActionListener(this);

		viewWorldItem = new JMenuItem("View World...");
		viewWorldItem.addActionListener(this);
		viewWorldItem.setEnabled(false);

		filemenu.add(newWorldItem);
		filemenu.add(quitItem);
		viewmenu.add(viewWorldItem);

		menubar.add(filemenu);
		menubar.add(viewmenu);
	}

	public MainFrame() {
		optionsPanel = new OptionsPanel(this);
		this.generator = new DataGenerator(optionsPanel);
		this.areaData = new ArrayList<String>();

		this.setTitle("Prediction Graph");
		setResizable(false);
		Toolkit tk = Toolkit.getDefaultToolkit();
		int xSize = ((int) tk.getScreenSize().getWidth());
		int ySize = ((int) tk.getScreenSize().getHeight());
		this.setSize(xSize, ySize);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// create a map panel
		mapPanel = new MapPanel();

		// Create a menu
		menubar = new JMenuBar();
		createMenu();
		this.setJMenuBar(menubar);

		// Create rest of the panels
		GridLayout layout = new GridLayout(2, 2);
		this.setLayout(layout);

		pastData = new GraphPanel("Past Data", "Hours since start",
				"Calculated Energy Consumption");
		actualData = new GraphPanel("Actual Data", "Hours since start",
				"Calculated Energy Consumption");

		neuralNetworkError = new GraphPanel("Neural Network Error",
				"Hours since start", "Error");

		this.add(pastData);
		this.add(neuralNetworkError);
		this.add(actualData);
		this.add(optionsPanel);
		hidePanels();
	}

	public void showPanels() {
		this.pastData.setVisible(true);
		this.actualData.setVisible(true);
		this.neuralNetworkError.setVisible(true);
		this.optionsPanel.setVisible(true);
	}

	public void hidePanels() {
		this.pastData.setVisible(false);
		this.actualData.setVisible(false);
		this.neuralNetworkError.setVisible(false);
		this.optionsPanel.setVisible(false);
	}

	public void startPastDataThread(JLabel currentValue) {
		generator.startUpdating(pastData, 24 * 365, RUN_STATE.PAST);
	}

	public void startTrainingThread() {
		generator.startUpdating(neuralNetworkError, 2, RUN_STATE.TRAIN);
	}

	public void startActualThread(JLabel estimateValue) {
		generator.startUpdating(actualData, 24 * 365, RUN_STATE.ACTUAL);
	}

	public void setupInitialisationData(Initialisation initData) {
		this.init = initData;
		this.areaData = initData.initialiseArea();
		generator.setArea(areaData);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();

		if (source == newWorldItem) {
			JFrame popupFrame = new JFrame("Create a new world");
			popupFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			popupFrame.setBounds(100, 100, 350, 230);
			popupFrame.setContentPane(new JDesktopPane());
			popupFrame.setLayout(new FlowLayout());
			initPanel = new InitialisationPanel(popupFrame, MainFrame.this);
			popupFrame.add(initPanel);
			popupFrame.setVisible(true);
			viewWorldItem.setEnabled(true);
		} else if (source == quitItem) {
			System.exit(0);
		} else if (source == viewWorldItem) {
			JFrame worldFrame = new JFrame("World");
			worldFrame.setResizable(false);
			worldFrame.setLocation(200, 200);
			worldFrame.setSize(400, 300);
			worldFrame.setVisible(true);
			worldFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			mapPanel.displayInitialisation(this.init);
			worldFrame.add(mapPanel);
		}
	}
	
	public static void main(String[] args) {
		MainFrame fr = new MainFrame();
		fr.setVisible(true);
	}
}
